<template>
  <div>
    <!-- <basic-container> -->
      <!-- <avue-form :option="option"
                 v-model="form"
                 @tab-click="handleTabClick"
                 @submit="handleSubmit"></avue-form> -->
      <avue-form :option="option" ref="passwordForm"
                 v-model="form"
                 @submit="handleSubmit"></avue-form>
    <!-- </basic-container> -->
  </div>
</template>

<script>
  import option from "@/option/user/info";
  import {
    getUserInfo, 
    updateInfo, 
    // updatePassword
  } from "@/api/system/user";
  import md5 from 'js-md5';
  import func from "@/util/func";
  import {updatePassword} from '@/api/famfrontend/accountManagement/accountManagement';
  import {mapGetters} from "vuex";


  export default {
    data() {
      return {
        index: 0,
        option: option,
        form: {}
      };
    },
    computed: {
      ...mapGetters(["userInfo"]),
    },
    created() {
      this.handleWitch();
    },
    methods: {
      handleSubmit(form, done) {
        // if (this.index === 0) {
        //   updateInfo(form).then(res => {
        //     if (res.data.success) {
        //       this.$message({
        //         type: "success",
        //         message: "修改信息成功!"
        //       });
        //     } else {
        //       this.$message({
        //         type: "error",
        //         message: res.data.msg
        //       });
        //     }
        //     done();
        //   }, error => {
        //     window.console.log(error);
        //     done();
        //   })
        // } else {
        //   updatePassword(md5(form.oldPassword), md5(form.newPassword), md5(form.newPassword1)).then(res => {
        //     if (res.data.success) {
        //       this.$message({
        //         type: "success",
        //         message: "修改密码成功!"
        //       });
        //       this.$emit("passwordBack","修改密码成功")
        //     } else {
        //       this.$message({
        //         type: "error",
        //         message: res.data.msg
        //       });
        //     }
        //     done();
        //   }, error => {
        //     window.console.log(error);
        //     done();
        //   })
        // }

        this.updatePassword(form, done)
      },
      handleWitch() {
        if (this.index === 0) {
          getUserInfo().then(res => {
            const user = res.data.data;
            this.form = {
              id: user.id,
              avatar: user.avatar,
              name: user.name,
              realName: user.realName,
              phone: user.phone,
              email: user.email,
            }
          });
        }
      },
      handleTabClick(tabs) {
        this.index = func.toInt(tabs.index);
        this.handleWitch();
      },


      updatePassword(form, done){
        let obj={
          id:this.userInfo.user_id,
          password:form.newPassword,
        }
        updatePassword(obj).then(res => {
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "修改密码成功!"
            });
            this.$emit("passwordBack","修改密码成功")
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            });
          }
          done();
        }, error => {
          window.console.log(error);
          done();
        })
      },
      beforeClose(){
        // console.log("beforeClose")
        this.$refs.passwordForm.resetFields();
      },



    }
  };
</script>

<style>
</style>
